package org.app.appjson.reader;

import org.app.appjson.reader.interfaces.Token;

/**
 * Json词法单元
 * @author lizhongsi
 * @version 1.0
 * @since 1.0
 */
public class JsonToken implements Token {

    /**
     * EOF of JSON document.
     * 文档结束
     */
    public static final byte END_DOCUMENT = 1;

    /**
     * Beginning of object: {
     * 对象开始
     */
    public static final byte BEGIN_OBJECT = 2;

    /**
     * End of object: }
     * 对象结束
     */
    public static final byte END_OBJECT = 3;

    /**
     * Beginning of array: [
     * 数组开始
     */
    public static final byte BEGIN_ARRAY = 4;

    /**
     * End of array: ]
     * 数组结束
     */
    public static final byte END_ARRAY = 5;

    /**
     * colon ':'，冒号
     */
    public static final byte SEP_COLON = 6;

    /**
     * comma ',' ，逗号
     */
    public static final byte SEP_COMMA = 7;

    /**
     * String value: "xxx"，字符串
     */
    public static final byte STRING = 8;

    /**
     * Number value: 123.456，数字
     */
    public static final byte NUMBER = 9;

    /**
     * Boolean value: true or false.布尔值：真或假
     */
    public static final byte BOOLEAN = 10;

    /**
     * null.
     */
    public static final byte NULL = 11;

    // 词法单元的类型。tokenType可以用枚举类型。由于仅底层使用，这里考虑性能和内存用byte
    private byte type;

    private String value;

    public byte getType() {
        return this.type;
    }

    public void setType(byte type) {
        this.type = type;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void clear() {
        this.type = -1;
        this.value = null;
    }

    public String getTypeValue() {
        switch (this.type) {
            case END_DOCUMENT: return "END_DOCUMENT";
            case BEGIN_OBJECT: return "BEGIN_OBJECT";
            case END_OBJECT: return "END_OBJECT";
            case BEGIN_ARRAY: return "BEGIN_ARRAY";
            case END_ARRAY: return "END_ARRAY";
            case SEP_COLON: return "SEP_COLON";
            case SEP_COMMA: return "SEP_COMMA";
            case STRING: return "STRING";
            case BOOLEAN: return "BOOLEAN";
            case NUMBER: return "NUMBER";
            case NULL: return "NULL";
            default: return null;
        }
    }

    @Override
    public String toString() {
        String value = this.value == null ? "null" : ("\"" + this.value + "\"");
        return "JsonToken{" +
                "tokenType=" + getTypeValue() +
                ", value=" + value +
                '}';
    }
}